package com.happyfamily.springboot.crowdsourcingplatform.controller.sun;

import com.google.gson.Gson;
import com.happyfamily.springboot.crowdsourcingplatform.annotation.OperationLogDetail;
import com.happyfamily.springboot.crowdsourcingplatform.enums.OperationType;
import com.happyfamily.springboot.crowdsourcingplatform.enums.OperationUnit;
import com.happyfamily.springboot.crowdsourcingplatform.model.ServerType;
import com.happyfamily.springboot.crowdsourcingplatform.model.TableModel;
import com.happyfamily.springboot.crowdsourcingplatform.service.sun.TypeService;
import io.swagger.annotations.ApiOperation;
import javafx.scene.control.Tab;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.swing.text.GapContent;
import java.util.List;

@Controller
@RequestMapping("/api-back/type/")
public class TypeController
{
	@Autowired
	private TypeService typeServiceImpl;

	@RequestMapping("getNeed")
	@ResponseBody
	public List<ServerType> getNeed()
	{
		return typeServiceImpl.getNeedType(null, null);
	}

	@RequestMapping("getServer")
	@ResponseBody
	public List<ServerType> getServer()
	{
		return typeServiceImpl.getServerType(null, null);
	}

	@RequestMapping("getNeedList")
	@ResponseBody
	@ApiOperation(value = "管理员获取所有需求类型", notes = "管理员获取所有需求类型", httpMethod = "POST", response = String.class)
	@OperationLogDetail(detail = "管理员获取所有需求类型", level = 1, operationUnit = OperationUnit.ADMIN, operationType = OperationType.SELECT)
	public String getNeedList(String limit, String page)
	{
		TableModel tableModel = new TableModel();
		tableModel.setCode(0);
		tableModel.setMsg("");
		tableModel.setCount(typeServiceImpl.getNeedSum());
		tableModel.setData(typeServiceImpl.getNeedType(Integer.valueOf(limit), Integer.valueOf(page)));
		Gson g = new Gson();
		return g.toJson(tableModel);
	}

	@RequestMapping("getServerList")
	@ResponseBody
	@ApiOperation(value = "管理员获取所有服务商类型", notes = "管理员获取所有服务商类型", httpMethod = "POST", response = String.class)
	@OperationLogDetail(detail = "管理员获取所有服务商类型", level = 1, operationUnit = OperationUnit.ADMIN, operationType = OperationType.SELECT)
	public String getServerList(String limit, String page)
	{
		TableModel tableModel = new TableModel();
		tableModel.setCode(0);
		tableModel.setMsg("");
		tableModel.setCount(typeServiceImpl.getSererSum());
		tableModel.setData(typeServiceImpl.getServerType(Integer.valueOf(limit), Integer.valueOf(page)));
		Gson g = new Gson();
		return g.toJson(tableModel);
	}

	@RequestMapping("getOtherList")
	@ResponseBody
	@ApiOperation(value = "管理员获取所有服务类型", notes = "管理员获取所有服务类型", httpMethod = "POST", response = String.class)
	@OperationLogDetail(detail = "管理员获取所有服务类型", level = 1, operationUnit = OperationUnit.ADMIN, operationType = OperationType.SELECT)
	public String getOtherList(String limit, String page)
	{
		TableModel tableModel = new TableModel();
		tableModel.setCode(0);
		tableModel.setMsg("");
		tableModel.setCount(typeServiceImpl.getOtherSum());
		tableModel.setData(typeServiceImpl.getOtherType(Integer.valueOf(limit), Integer.valueOf(page)));
		Gson g = new Gson();
		return g.toJson(tableModel);
	}

	@RequestMapping("checkServer")
	@ResponseBody
	@ApiOperation(value = "管理员检查需求类型是否存在", notes = "管理员检查需求类型是否存在", httpMethod = "POST", response = String.class)
	@OperationLogDetail(detail = "管理员检查需求类型是否存在", level = 1, operationUnit = OperationUnit.ADMIN, operationType = OperationType.SELECT)
	public String checkServer(String sname)
	{
		String str = "no";
		boolean flag = typeServiceImpl.checkServer(sname);
		if (flag)
		{
			str = "yes";
		}
		return str;
	}

	@RequestMapping("checkNeed")
	@ResponseBody
	@ApiOperation(value = "管理员检查服务商类型是否存在", notes = "管理员检查服务商类型是否存在", httpMethod = "POST", response = String.class)
	@OperationLogDetail(detail = "管理员检查服务商类型是否存在", level = 1, operationUnit = OperationUnit.ADMIN, operationType = OperationType.SELECT)
	public String checkNeed(String sname)
	{
		String str = "no";
		boolean flag = typeServiceImpl.checkNeed(sname);
		if (flag)
		{
			str = "yes";
		}
		return str;
	}

	@RequestMapping("checkOther")
	@ResponseBody
	@ApiOperation(value = "管理员检查服务类型是否存在", notes = "管理员检查服务类型是否存在", httpMethod = "POST", response = String.class)
	@OperationLogDetail(detail = "管理员检查服务类型是否存在", level = 1, operationUnit = OperationUnit.ADMIN, operationType = OperationType.SELECT)
	public String checkOther(String sname)
	{
		String str = "no";
		boolean flag = typeServiceImpl.checkOther(sname);
		if (flag)
		{
			str = "yes";
		}
		return str;
	}

	@RequestMapping("addServerType")
	@ResponseBody
	@ApiOperation(value = "管理员添加新的服务商类型", notes = "管理员添加新的服务商类型", httpMethod = "POST", response = String.class)
	@OperationLogDetail(detail = "管理员添加新的服务商类型", level = 3, operationUnit = OperationUnit.ADMIN, operationType = OperationType.INSERT)
	public String addServerType(String sname)
	{
		String str = "no";
		boolean flag = typeServiceImpl.addServerType(sname);
		if (flag)
		{
			str = "yes";
		}
		return str;
	}

	@RequestMapping("addNeedType")
	@ResponseBody
	@ApiOperation(value = "管理员添加新的需求类型", notes = "管理员添加新的需求类型", httpMethod = "POST", response = String.class)
	@OperationLogDetail(detail = "管理员添加新的需求类型", level = 3, operationUnit = OperationUnit.ADMIN, operationType = OperationType.INSERT)
	public String addNeedType(String sname)
	{
		String str = "no";
		boolean flag = typeServiceImpl.addNeedType(sname);
		if (flag)
		{
			str = "yes";
		}
		return str;
	}

	@RequestMapping("addOtherType")
	@ResponseBody
	@ApiOperation(value = "管理员添加新的服务类型", notes = "管理员添加新的服务类型", httpMethod = "POST", response = String.class)
	@OperationLogDetail(detail = "管理员添加新的服务类型", level = 3, operationUnit = OperationUnit.ADMIN, operationType = OperationType.INSERT)
	public String addOtherType(String sname)
	{
		String str = "no";
		boolean flag = typeServiceImpl.addOtherType(sname);
		if (flag)
		{
			str = "yes";
		}
		return str;
	}

	@RequestMapping("deleteServerType")
	@ResponseBody
	@ApiOperation(value = "管理员删除某个服务商类型", notes = "管理员删除某个服务商类型", httpMethod = "POST", response = String.class)
	@OperationLogDetail(detail = "管理员删除某个服务商类型", level = 5, operationUnit = OperationUnit.ADMIN, operationType = OperationType.ALL)
	public String deleteServerType(String sid)
	{
		String str = "no";
		int n = typeServiceImpl.getSererSum();
		if (n > 3)
		{
			boolean flag = typeServiceImpl.deleteServerType(Long.valueOf(sid));
			if (flag)
			{
				str = "yes";
			}
		}
		return str;
	}

	@RequestMapping("deleteNeedType")
	@ResponseBody
	@ApiOperation(value = "管理员删除某个需求类型", notes = "管理员删除某个需求类型", httpMethod = "POST", response = String.class)
	@OperationLogDetail(detail = "管理员删除某个需求类型", level = 5, operationUnit = OperationUnit.ADMIN, operationType = OperationType.ALL)
	public String deleteNeedType(String sid, String sname)
	{
		String str = "no";
		int n = typeServiceImpl.getNeedSum();
		if (n > 5)
		{
			boolean flag = typeServiceImpl.deleteNeedType(Long.valueOf(sid), sname);
			if (flag)
			{
				str = "yes";
			}
		}
		return str;
	}

	@RequestMapping("deleteOtherType")
	@ResponseBody
	@ApiOperation(value = "管理员删除某个服务类型", notes = "管理员删除某个服务类型", httpMethod = "POST", response = String.class)
	@OperationLogDetail(detail = "管理员删除某个服务类型", level = 5, operationUnit = OperationUnit.ADMIN, operationType = OperationType.ALL)
	public String deleteOtherType(String sid)
	{
		String str = "no";
		int n = typeServiceImpl.getOtherSum();
		if (n > 3)
		{
			boolean flag = typeServiceImpl.deleteOtherType(Long.valueOf(sid));
			if (flag)
			{
				str = "yes";
			}
		}
		return str;
	}
}
